<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\Attachment;
use fast\Date;
use think\Db;

/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index(){
        try {
            \think\Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {

        }
        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379);
        $redis->select(1);
        $admin_id = $this->auth->id;

        //今日数据
        $times_today = 0;
        $users_today = 0;
        $ips_today = 0;

        $times_30 = [];
        $users_30 = [];
        $ips_30 = [];

        $times_24 = [];

        $area_34 = [];

        $date = date('Y-m-d');
        $date_start = date('Y-m-d',strtotime('-29 days'));
        $date_range = printDates($date_start,$date);

        if($admin_id == 1){
            $admin_count = Admin::count();
            $group_list = Db::name('short_chain_group')->field('id')->where('status','=',1)->column('id');
            //今日数据

            $times_today = $redis->get('short_chain_times_pv_'.$date);
            $users_today = $redis->sCard('short_chain_user_'.$date);
            $ips_today = $redis->sCard('short_chain_ip_'.$date);

//            foreach($group_list as $k=>$v){
//                $times_today += $redis->zScore('short_chain_times_'.$date,$v);
//                $users_today += $redis->sCard('short_chain_user_'.$date.'_'.$v);
//                $ips_today += $redis->sCard('short_chain_ip_'.$date.'_'.$v);
//            }
            //访问曲线
            foreach($date_range as $k1=>$v1){
                $times_30[$v1] = 0;
                $users_30[$v1] = 0;
                $ips_30[$v1]   = 0;
                $times_30[$v1] = $redis->get('short_chain_times_pv_'.$v1);
                $users_30[$v1] = $redis->sCard('short_chain_user_'.$v1);
                $ips_30[$v1]   = $redis->sCard('short_chain_ip_'.$v1);

//                foreach($group_list as $k2=>$v2){
//                    $times_30[$v1] += $redis->zScore('short_chain_times_'.$v1,$v2);
//                    $users_30[$v1] += $redis->sCard('short_chain_user_'.$v1.'_'.$v2);
//                    $ips_30[$v1]   += $redis->sCard('short_chain_ip_'.$v1.'_'.$v2);
//                }
            }
            //24小时分布
            $hours_24 = ['00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23'];
            foreach($hours_24 as $k3=>$v3){
                $times_hour[$v3] = 0;
                $times_hour[$v3] += $redis->zCard('short_chain_times_hour_'.$date.'_'.$v3);
//                foreach($group_list as $k4=>$v4){
//                    $times_hour[$v3] += $redis->zScore('short_chain_times_hour_'.$date.'_'.$v3,$v4);
//                }
                $times_24[$v3] = $times_hour[$v3];
            }
            //地区分布
            $province_list = Db::name('district')->field('name')->where('type','=',1)->column('name');
            foreach($province_list as $k5=>$v5){
                $province_value[$v5] = 0;
                $province_value[$v5] += $redis->zCard('short_chain_area_'.$date.'_'.$v5);
//                foreach($group_list as $k6=>$v6){
//                    $province_value[$v5] += $redis->zScore('short_chain_area_'.$date.'_'.$v5,$v6);
//                }
                $area_34[] = [
                    'name'=>$v5,
                    'value'=>$province_value[$v5],
                ];
            }
            //高频IP
            $most_ip = $redis->zRevRange('short_chain_IP_'.$date,'0','9',true);

        }else{
            $admin_count = 0;
            $group_list = Db::name('short_chain_group')->field('id')->where('status','=',1)
                ->where('admin_id','=',$admin_id)->column('id');
            //今日数据
            foreach($group_list as $k=>$v){
                $times_today += $redis->zScore('short_chain_times_'.$date,$v);
                $users_today += $redis->sCard('short_chain_user_'.$date.'_'.$v);
            }
            //访问曲线
            foreach($date_range as $k1=>$v1){
                $times_30[$v1] = 0;
                $users_30[$v1] = 0;
                $ips_30[$v1] = 0;
                foreach($group_list as $k2=>$v2){
                    $times_30[$v1] += $redis->zScore('short_chain_times_'.$v1,$v2);
                    $users_30[$v1] += $redis->sCard('short_chain_user_'.$v1.'_'.$v2);
                    $ips_30[$v1]   += $redis->sCard('short_chain_ip_'.$v1.'_'.$v2);
                }
            }
            //24小时分布
            $hours_24 = ['00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23'];
            foreach($hours_24 as $k3=>$v3){
                $times_hour[$v3] = 0;
                foreach($group_list as $k4=>$v4){
                    $times_hour[$v3] += $redis->zScore('short_chain_times_hour_'.$date.'_'.$v3,$v4);
                }
                $times_24[$v3] = $times_hour[$v3];
            }
            //地区分布
            $province_list = Db::name('district')->field('name')->where('type','=',1)->column('name');
            foreach($province_list as $k5=>$v5){
                $province_value[$v5] = 0;
                foreach($group_list as $k6=>$v6){
                    $province_value[$v5] += $redis->zScore('short_chain_area_'.$date.'_'.$v5,$v6);
                }
                $area_34[] = [
                    'name'=>$v5,
                    'value'=>$province_value[$v5],
                ];
            }
            //高频IP
            $most_ip = $redis->zRevRange('short_chain_IP_'.$date.'_'.$group_list[0],'0','50',true);

        }
//        dump($times_24);die;
        //今日访问次数，今日访问人数，总管理员数
        $this->view->assign([
            'times_today'         => $times_today,
            'users_today'         => $users_today,
            'ips_today'         => $ips_today,
            'admin_count'         => $admin_count,
            'most_ip'         => $most_ip,
        ]);
        $this->assignconfig('times_30', array_values($times_30));
        $this->assignconfig('users_30', array_values($users_30));
        $this->assignconfig('ips_30', array_values($ips_30));
        $this->assignconfig('times_24', array_values($times_24));
        $this->assignconfig('area_34', $area_34);
        $this->assignconfig('date_range', $date_range);

        return $this->view->fetch();
    }


}
